package com.wtgroup.demo.leetcode.q035_搜索插入位置;

/**
 * @author 60906
 * @date 2021/5/16 17:51
 */
public class Q035 {


    /**
     * 关于没有匹配到的处理, LC 处理巧妙些
     */
    class Solution {
        public int searchInsert(int[] nums, int target) {
            int N = nums.length;
            if (N == 0) {
                return 0;
            }
            int left = 0, right = N-1;
            int mid = 0;
            while (left <= right) {
                mid = (left+right) / 2;
                if (target < nums[mid]) {
                    right = mid - 1;
                } else if (target > nums[mid]) {
                    left = mid + 1;
                } else {
                    return mid;
                }
            }

            // 没有匹配到
            return target > nums[mid] ? mid+1 : mid;
        }
    }


}
